import cv2
import numpy as np
img = cv2.imread('cat.png')
cv2.imshow('show', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

def nearest_interpolation_v2(src, dst_size):
  src_h, src_w, channel = src.shape
  dst_w, dst_h = dst_size
  x_scale = dst_w / src_w
  y_scale = dst_h / src_h
  dst_y, dst_x = np.mgrid[:dst_h, :dst_w]
  src_x = np.around((dst_x + 0.5) / x_scale - 0.5).astype(np.int64)
  src_y = np.around((dst_y + 0.5) / y_scale - 0.5).astype(np.int64)
  src_x = np.clip(src_x, 0, src_w-1)
  src_y = np.clip(src_y, 0, src_h-1)
  dst = src[src_y, src_x].copy()
  return dst

img_n2 = nearest_interpolation_v2(img, (512, 512))
cv2.imshow('show', img_n2)
cv2.waitKey(0)
cv2.destroyAllWindows()